package wb.is.besti.edu.cd.emergency_responsesys.utils;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import wb.is.besti.edu.cd.emergency_responsesys.R;

public class HttpUtil {

    public  List<Events> getMarkerList(){
        String path="http://152.136.46.15/NewsJson/MarkerServlet";
        HttpURLConnection connection = null;
        List<Events> eventsList = new ArrayList<>();
        try {
            connection = (HttpURLConnection)new URL(path).openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(8000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            if (connection.getResponseCode()==HttpsURLConnection.HTTP_OK){
                InputStream json = connection.getInputStream();
                eventsList = initMarkerJSON(json);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return eventsList;

    }

    private static List<Events> initMarkerJSON(InputStream inputStream) throws Exception{
        List<Events> list = new ArrayList<>();
        byte[] data = StreamTool.read(inputStream);
        String json = new String(data);
        System.out.print(json);
        JSONArray jsonArray = new JSONArray(json);
        for (int i=0;i<jsonArray.length();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String title = jsonObject.getString("title");
            String time = jsonObject.getString("time");
            String des = jsonObject.getString("des");
            String position = jsonObject.getString("position");
            double longitude = jsonObject.getDouble("longitude");
            double latitude = jsonObject.getDouble("latitude");
            list.add(new Events(time,position,title,des,longitude,latitude));
        }
        System.out.println(list);
        return list;
    }

    public static int EventsPost(String event){
        String path="http://152.136.46.15/NewsJson/EventServlet";
        int flag = 0;
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection)new URL(path).openConnection();
            connection.setConnectTimeout(3000);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type","application/json");
            connection.setRequestProperty("charset","UTF-8");
            connection.setRequestProperty("accept","application/json");
            OutputStream outputStream = connection.getOutputStream();
            byte[] data = event.toString().getBytes();
            outputStream.write(data);
            outputStream.flush();
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK){
               flag = 1;
               Log.i("test","成功");
            }
            else {
                flag=2;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            if (connection !=null){
                connection.disconnect();
            }
        }
        return flag;
    }
}
